<!DOCTYPE html>
<html lang="en">
<head>
    <!--
    @Author 硕鼠
    @Date 2020/4/11 9:28
    @Version 1.0
    @Description 文件说明
  -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">layui 后台布局</div>
        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item"><a href="">控制台</a></li>
            <li class="layui-nav-item"><a href="">商品管理</a></li>
            <li class="layui-nav-item"><a href="">用户</a></li>
            <li class="layui-nav-item">
                <a href="javascript:;">其它系统</a>
                <dl class="layui-nav-child">
                    <dd><a href="">邮件管理</a></dd>
                    <dd><a href="">消息管理</a></dd>
                    <dd><a href="">授权管理</a></dd>
                </dl>
            </li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <img src="http://t.cn/RCzsdCq" class="layui-nav-img">
                    贤心
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="">退了</a></li>
        </ul>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree"  lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">新闻管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="http://localhost:8080/userInsert.html">增加新闻</a></dd>
                        <dd><a href="">新闻列表</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>

    <div class="layui-body">
        <!-- 内容主体区域-需要编写 -->
        <div style="padding: 15px;">
           <table id="page" lay-filter = "demo"></table>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script src="layui/layui.js"></script>
<script>
    layui.use(['element','table','jquery'], function(){
        var element = layui.element;
        var $ = layui.jquery;
        var table = layui.table;

        table.render({
            elem:'#page',
            page:true,
            url:'http://localhost:8080/newsList',
            cols: [
                [ //表头
                {field: 'id', title: '编号', width:200, sort: true, fixed: 'left'}
                ,{field: 'title', title: '标题', width:200}
                ,{field: 'author', title: '作者', width:200}
                ,{field: 'note', title: '内容', width:200}
                ,{fixed: 'right',align:'center', width:200,toolbar:'#barDemo'}
                ]
            ]
        });

        //对事件进行监听
        table.on('tool(demo)',function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'delete'){  //删除操作
                layer.confirm('真的删除行么', function(index){
                    obj.del();   //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    //地址：http://localhost:8080/delete?id=3
                    var location = 'http://localhost:8080/delete?id='+data.id;

                    table.render({  //重新进行渲染，阻止页面跳转
                        elem:'#page',
                        page:true,
                        url:location,
                        cols: [
                            [ //表头
                                {field: 'id', title: '编号', width:200, sort: true, fixed: 'left'}
                                ,{field: 'title', title: '标题', width:200}
                                ,{field: 'author', title: '作者', width:200}
                                ,{field: 'note', title: '内容', width:200}
                                ,{fixed: 'right',align:'center', width:200,toolbar:'#barDemo'}
                            ]
                        ]
                    });
                });
            }else if(layEvent === 'edit'){
                //do something 跳转到这个界面
                window.location = 'http://localhost:8080/UserEdit?id='+data.id

                //同步更新缓存对应的值
                obj.update({
                    username: '123'
                    ,title: 'xxx'
                });
            }
        });
    });
</script>
<!--toolbar 对应的模板，（绑定工具条模板）lay-event打错-->
<script type="text/html" id="barDemo">
    <a  class="layui-btn layui-btn-xs layui-btn-primary"  lay-event="edit">编辑</a>
    <a  class="layui-btn layui-btn-xs layui-btn-danger"  lay-event="delete">删除</a>

</script>
</body>
</html>